<?php

namespace App\Http\Controllers\Api;

use App\Models\{GoodsBase, GoodsCommon, GoodsCommonDetail, GoodsImages, GoodsPropertyValue};
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public function show(GoodsBase $goodsBase)
    {
        // 增加商品点击数
        $goodsBase->goods_click += 1;
        $goodsBase->save();

        // 商品信息（商品活动信息，评论数，销售数，咨询数）
        // 商品活动信息
        $promotions = $goodsBase->getPromotions();
        // 商品 Common 信息
        $common = GoodsCommon::find($goodsBase->common_id);
        if ($common && $common->common_property) {
            $common_property_row = [];
            $goods_common = $common->toArray();
            foreach($common->common_property as $v) {
                $goodsPropertyValue = GoodsPropertyValue::find($v['1']);
                $common_property_row[$v['0']] = $goodsPropertyValue->property_value_name;
            }
            $goods_common['common_property_row'] = $common_property_row;

            $goodsCommonDetail = GoodsCommonDetail::find($goodsBase->common_id);
            $goods_common['common_detail'] = $goodsCommonDetail ? $goodsCommonDetail->common_body : '';
            $goods_common['common_spec_value_c'] = $common->common_spec_value;
            if (is_array($common->common_spec_value)) {
                foreach ($common->common_spec_value as $comvk => $comvv) {
                    // 所有商品颜色规格图片只有一个有默认值1，其它规格找出对应规格第一张图片剪切
                    foreach ($comvv as $cvk => $cvv) {
                        $image_default = 0;
                        $image_common_cond['images_color_id'] = $cvk;
                        $image_common_row = GoodsImages::getGoodsImage($image_common_cond);
                        foreach($image_common_row as $ik=>$iv) {
                            $image_default += $iv['images_is_default'];
                            if ($iv['images_is_default'] == 1) {
                                $img_default_key = $ik;
                            }
                        }

                        if ($image_default == 0) {
                            $image_common_row = array_values($image_common_row)[0];
                        } else {
                            $image_common_row = $image_common_row[$img_default_key];
                            unset($img_default_key);
                        }

                        if ($image_common_row) {
                            $goods_common['common_spec_value'][$comvk][$cvk] = image_thumb($image_common_row['images_image'],42,42);
                            $goods_common['common_spec_value_color'][$cvk] = image_thumb($image_common_row['images_image'], 360, 360);
                        }
                    }

                }
            }
        }

    }
}
